$(function () {
    $("#join_shop_car").click(function () {
        //添加购物车
        var product_id = $("#join_shop_car").attr("product-id");
        var count = $("#number").val();


        params = {
            'product_id': product_id,
            'count': count
        };


        $.ajax({

            url: "/shop/add_shop_car",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json"


        }).success(function (resp) {
            if (resp.errno == '0') {
                alert('已添加到购物车');
                location.reload()
            } else {
                alert(resp.errmsg);
                location.reload()
            }
        }).error(function () {
            alert('网络异常')
        })


    });

    jian = function (obj) {
        //减数量
        var number = $(obj).next();
        var count = number.val();
        if (count > 1) {
            number.attr("value", Number(count) - 1)
        } else {
            alert('加入购物车数量最少一份')
        }

    };

    jia = function (obj) {
        //加数量
        var number = $(obj).prev();
        var count = number.val();
        if (count < 999) {
            number.attr("value", Number(count) + 1)
        } else {
            alert('数量不能大于999')
        }

    };

    remove_shop = function (obj) {
        //删除购物车
        var product_id = $(obj).attr("product-id");
        params = {
            'product_id': product_id
        };
        $.ajax({

            url: "/shop/remove_shop_car",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json"
        }).success(function (resp) {
            if (resp.errno == '0') {
                location.reload()
            } else {
                alert(resp.errmsg)
            }
        }).error(function () {
            alert('网络异常')
        })
    };

    check_all = function (obj) {
        // 全选的逻辑
        if ($(obj).attr('checked') == 'checked') {
            $("[name='rememberMe']").attr("checked", 'checked')
        } else {
            $("[name='rememberMe']").attr("checked", false)
        }

    };

    shop_click = function (obj) {
        // 购物车内多选框的js
        var status = $(obj).attr('checked');

        var all_check = $("input[type='checkbox']");
        var if_all_check = false;
        if (status != 'checked') {
            $("#rememberMe").attr('checked', false)
        }
        for (var i = 1; i < all_check.length; i++) {
            if ($(all_check[i]).attr('checked') != 'checked') {
                if_all_check = false;
                return;
            }
            if_all_check = true;
        }
        if (if_all_check == true) {
            $("#rememberMe").attr('checked', 'checked')
        }

    };


    now_pay = function (obj) {
        // 购物车内购买商品跳转到订单页
        var ready_pay_list = $("input:checkbox:checked");
        var product_id_list = {};
        for (var i = 0; i < ready_pay_list.length; i++) {
            product_id_list[$(ready_pay_list[i]).attr('product-id')] = $(ready_pay_list[i]).attr('product-count')
        }
        var params = {
            'product_id_list': product_id_list
        };
        $.ajax({
            url: "/shop/if_pay",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json"
        }).success(function (resp) {
            if (resp.errno == '0') {
                location.href = '/shop/order_form?is_car=yes'
            } else {
                alert(resp.errmsg)
            }
        })

    };

    detail_buy = function (obj) {
        // 详情页购买商品跳转订单页
        var product_id = $(obj).attr('product-id');
        var count = $("#number").val();
        var product_id_list = {};
        product_id_list[product_id] = count;
        var params = {
            'product_id_list': product_id_list,
        };
        $.ajax({
            url: "/shop/if_pay",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json"
        }).success(function (resp) {
            if (resp.errno == '0') {
                location.href = '/shop/order_form?count=' + count
            } else {
                alert(resp.errmsg)
            }
        })
    };

});